﻿using Qy.Common;
using Qy.Models;
using PetaPoco;
using System.Threading.Tasks;

namespace Qy.Repository
{
    [AppService]
    public class NoticeRepository : BaseRepository<Notice, PetaPocoContext>
    {
        public NoticeRepository(PetaPocoContext petaPocoContext) : base(petaPocoContext) { }

        /// <summary>
        /// 查询一条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ViewNotice> GetAndUserAsync(int id)
        {
            Sql sql = Sql.Builder;
            sql.Select("notice_key.*,getuser.Nickname as GetNickname,getuser.Avatar as GetUserAvatar," +
                "postuser.Nickname as PostNickname,postuser.Avatar as PostAvatar," +
                "notice.Title,notice.Type,notice.Content,notice.PostUid,notice.PostTime,notice.Id as NoticeId,notice.Att")
                .From("qy_notice as notice")
                .LeftJoin("qy_notice_key as notice_key").On("notice_key.Nid = notice.Id")
                .LeftJoin("qy_admin_user as getuser").On("notice_key.GetUid = getuser.Id");
            sql.LeftJoin("qy_admin_user as postuser").On("notice.PostUid = postuser.Id");
            sql.Where("notice.Id =@0", id);

            return await CreateDAO().FirstOrDefaultAsync<ViewNotice>(sql);
        }
    }
}
